package com.minshenglife.zanzan.web.weixin.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.WorkCon;
import com.minshenglife.zanzan.entity.WorkConPower;
import com.minshenglife.zanzan.pub.Config;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.pub.message.MessageUtil;
import com.minshenglife.zanzan.pub.message.WXRespMessage;
import com.minshenglife.zanzan.pub.message.custservice.CS_TextMessage;
import com.minshenglife.zanzan.service.UserService;
import com.minshenglife.zanzan.service.WorkConPowerService;
import com.minshenglife.zanzan.service.WorkConService;
import com.nationsky.pub.utils.StringUtils;


/**
 * 押宝专用包批处理service
 * @author huodd
 *
 */
@Component
@Transactional
public class WXWorkLogService {
	Logger log = LoggerFactory.getLogger(WXWorkLogService.class);
	@Resource
	WorkConService workConService;
	@Resource
	UserService userService;
	@Resource
	WXAccTokenService tokenService;
	@Resource
	WorkConPowerService workConPowerService; 
	/**可以被审阅的用户*/
	private final String CANBEREVIEWUSER="NC_CanBeReviewUser";
	/**
	 * 批处理：对未发送工作日志的指定员工发送提示信息
	 */
	public void batchSendPromptInfoOfWorkLog() {
		log.debug("start 批处理发送提示信息  开始运行。。。。。");
		String startDate="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar c=Calendar.getInstance();
		int wk=c.get(Calendar.DAY_OF_WEEK);
		if(wk==7||wk==1){
			log.debug("end 周六日不推送消息。。。。。");
			return ;
		}
		if(wk==2){
			c.add(Calendar.DAY_OF_MONTH, -3);
		}else{
			c.add(Calendar.DAY_OF_MONTH, -1);
		}
		startDate=sdf.format(c.getTime());
		try {
			//String canBeReviewUser=Config.getProperty(CANBEREVIEWUSER);
			//add by jianghaibo 2015-02-03
			String canBeReviewUser = workConPowerService.findPowerUserByType(CANBEREVIEWUSER);
			
			String workConType=Keys.WORK_CONTENT_NORMAL;
			String logDate=startDate;
			//获取前一个工作日未填写日志的人员
			List<WorkCon> list=workConService.findWorkConListOfDate(canBeReviewUser, workConType, logDate);
			String[] arr= canBeReviewUser.split("\\|");
			Map<String ,String> map =new HashMap<String,String>();
			for(int i=0;i<arr.length;i++){
				map.put(arr[i], arr[i]);
			}
			for(WorkCon con : list){
				if(con.getEmpId()!=null&&map.containsKey(con.getEmpId())){
					map.remove(con.getEmpId());
				}
			}
			Set<String> set=map.keySet();
			String str_time=new SimpleDateFormat("MM月dd日").format(c.getTime());
			for(String str :set){
				if(StringUtils.isNullOrEmpty(str)){
					continue;
				}
				log.info("===========未填写日志: "+str);
				User user=userService.getUserInfo(str);
				if(StringUtils.isNullOrEmpty(user.getWXCode())){
					log.info("===========未绑定员工号，不推送消息: "+str);
					continue;
				}
				try{
					String to_message="早上好！您还未填写"+str_time+"的工作日志，请尽快填写。";
					CS_TextMessage tm=new CS_TextMessage();
					tm.setTouser(user.getWXCode());
					tm.setMsgtype(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
					tm.setText(to_message);
					WXRespMessage respMessage=WXBaseService.sendCustServiceMessage(tm,tokenService.getWXAccToken());
					log.info("==========="+respMessage.getErrcode());
					System.out.println("==========="+respMessage.getErrcode());
					if("0".equals(respMessage.getErrcode())){
						log.info("客服消息发送成功");
					}else{
						log.info("客服消息发送失败，错误信息："+respMessage.getErrmsg());
					}
				} catch (Exception e) {
					e.printStackTrace();
					log.error("给"+str+"推送消息失败！");
				}
			}
			log.debug("批处理发送提示信息运行成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.debug("end 批处理发送提示信息  运行结束。。。。。");
	}
	
	public static void main(String[] asdf){
		String endDate="";
		String startDate="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar c=Calendar.getInstance();
		System.out.println("===="+c.get(Calendar.DAY_OF_WEEK));
		endDate=sdf.format(c.getTime());
		c.add(Calendar.DAY_OF_MONTH, -7);
		System.out.println("===="+c.get(Calendar.DAY_OF_WEEK));
		startDate=sdf.format(c.getTime());
		System.out.println(endDate);;
		System.out.println(startDate);
	}
}
